<template>
	<view class="page">
		<u-navbar title="登录" :placeholder="true" :titleStyle="titleStyle" leftIconColor="#fff" :bgColor="bgColor"
			:autoBack="true">
			<!-- <view class="u-nav-slot" slot="right"><span class="registerText" @click="toPage('/pages/register/index')">注册</span></view> -->
		</u-navbar>

		<view class="login_form">
			<image src="../../static/images/logo.png" mode="widthFix" style="width: 280rpx;height: auto;margin: 30rpx auto;display: block;"></image>
			<view class="list">
				<view class="label main-text-color">用户名</view>
				<u--input type="text" fontSize="15" border="none" placeholder="请输入用户名"
					v-model="form.username"></u--input>
			</view>
			<view class="list">
				<view class="label main-text-color">密码</view>
				<u--input type="password" fontSize="15" border="none" placeholder="请输入密码"
					v-model="form.password"></u--input>
			</view>
		</view>

		<view class="agree" @click="agreeCheck = !agreeCheck">
			<image v-if="agreeCheck" src="../../static/images/radio_check.png" mode="widthFix"></image>
			<image v-else src="../../static/images/radio.png" mode="widthFix"></image>
			<view class="agree_text grey">
				我已阅读并同意我和我的家
				<span class="main-text-color" @click.stop="toPage('/pages/register/xieyi')">用户协议</span>
				和
				<span class="main-text-color" @click.stop="toPage('/pages/register/yinsi')">隐私政策</span>
			</view>
		</view>

		<view class="flexLogin">
			<view class="loginBtn main-bg-color" @click="submit">登 录</view>
		</view>

		<view class="forget main-text-color" style="padding: 0 20%;margin-top: 30rpx;">
			<!-- <view @click="toPage('/pages/login/loginbycode')">手机号登录</view> -->
			<view @click="wxRegister">注册</view>
			<view @click="toPage('/pages/changeMy/forgetpwd')">忘记密码？</view>
		</view>

		<view class="otherLogin">
			<u-divider text="使用第三方账号登录"></u-divider>
			<view class="btns">
				<!-- 	<view>
					<image src="../../static/images/qq.png" mode="widthFix"></image>
					<span>QQ登录</span>
				</view> -->
				<!-- #ifdef MP-WEIXIN -->
				<!-- <view>
					<button type="primary" class="getUserInfobtn" open-type="getUserInfo"
						@getuserinfo="mpGetUserInfo"></button>
					<image src="../../static/images/wechat.png" mode="widthFix"></image>
					<span>微信登录</span>
				</view> -->
				<view>
					<view class="getUserInfobtn"  @click="submitlogin"></view>
					<image src="../../static/images/wechat.png" mode="widthFix"></image>
					<span>微信登录</span>
				</view>
				<!-- #endif -->

			</view>
		</view>
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="getNumber">
				<view class="title">
				 <image src="../../static/images/logo.png" mode="widthFix"></image>	MyFamilyLove 申请
				</view>
				<view class="desc">
					申请获取您微信绑定的手机号
				</view>
				<view class="btns">
					<span @click="login">拒绝</span>
					<span class="act" >允许
					<button class="getPhoneNumberbss" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
					</span>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: '#08C060',
				titleStyle: {
					color: '#fff'
				},
				form: {
					username: '',
					password: ''
				},
				agreeCheck: false,
				weiloginForm: {
					code: '',
					openId: '',
					otherAvatarUrl: '',
					otherNickname: '',
					otherPhone: '',
					otherSex: '',
					otherUsername: '',
					provider: 'wxmp',
				}
			};
		},
		onLoad() {
			
		},
		methods: {
			wxRegister() {
				uni.login({
					provider:"weixin",
					success: (res) => {
						console.log(res.code)
						let provider = ''
						// #ifdef MP-WEIXIN
						provider = 'wxmp'
						// #endif
						// #ifdef APP-PLUS
						provider = 'wx'
						// #endif
						uni.navigateTo({
							url: `/pages/register/index?code=${res.code}&provider=${provider}`
						})
					}
				})
			},
			toPage(url) {
				uni.navigateTo({
					url
				});
			},
			submitlogin(){
				
				if (!this.agreeCheck) {
					return this.$Show.show('请勾选用户协议及隐私政策')
				}
				
				
				
				uni.login({
					success: loginRes => {
						this.$api.loginloginByWp2({
							code: loginRes.code,
							provider:'wxmp'
						}).then(res2 => {
							console.log(res2)
						
						uni.setStorageSync('token', res2.token)
						uni.setStorageSync('familyId', res2.familyIds)
						uni.setStorageSync('nickname', res2.nickname)
						uni.setStorageSync('username', res2.username)
						uni.setStorageSync('showPayment', res2.showPayment)
												
						uni.setStorageSync('userNewCalendar', res2.userNewCalendar)
												
						uni.setStorageSync('maxImageMegBytes', res2.maxImageMegBytes)
						uni.setStorageSync('maxVideoMegBytes', res2.maxVideoMegBytes)
						uni.setStorageSync('systemUser', res2.systemUser)
						uni.reLaunch({
							url: '/pages/tabbar/index/index'
						})
							// if (res2.token) {
								
							// } else {
								
							// 	this.$refs.popup.open()
								
							// }
						})
						
						
					}
				})
			},
			getPhoneNumber(e) {
				
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					let data = {
						code: this.weiloginForm.code,
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
					}
					this.$api.getWxmpPhoneNo(data).then(res => {
						this.weiloginForm.otherPhone = res.phoneNumber
						this.login()
					})
				}else{
					this.login()
				}
				
			
			},
			login() {
				this.$api.loginloginByWp2(this.weiloginForm).then(res => {
					uni.setStorageSync('token', res.token)
					uni.setStorageSync('familyId', res.familyIds)
					uni.setStorageSync('nickname', res.nickname)
					uni.setStorageSync('username', res.username)
					uni.setStorageSync('showPayment', res.showPayment)
					uni.setStorageSync('userNewCalendar', res.userNewCalendar)
			
					uni.setStorageSync('maxImageMegBytes', res.maxImageMegBytes)
					uni.setStorageSync('maxVideoMegBytes', res.maxVideoMegBytes)
					uni.setStorageSync('systemUser', res.systemUser)
			
			
			
					let menberdata = uni.getStorageSync('menberdata') ? uni.getStorageSync('menberdata') : ''
					uni.hideLoading()
					if (menberdata) {
						console.log(menberdata)
			
						this.$api.acceptInvite(menberdata).then(res => {
							this.$Show.show('家庭加入成功')
							uni.removeStorageSync('menberdata')
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							}, 1500)
						})
					} else {
						console.log(res)
						uni.reLaunch({
							url: '/pages/tabbar/index/index'
						})
					}
			
				})
			},
			submit() {
				if (this.form.username == '') {
					return this.$Show.show('请输入用户名')
				}


				if (this.form.password == '') {
					return this.$Show.show('请输入密码')
				}

				if (!this.agreeCheck) {
					return this.$Show.show('请勾选用户协议及隐私政策')
				}

				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				this.$api.loginusername(this.form).then(res => {
					console.log(res)
					uni.setStorageSync('token', res.token)
					uni.setStorageSync('familyId', res.familyIds)
					uni.setStorageSync('nickname', res.nickname)
					uni.setStorageSync('username', res.username)
					uni.setStorageSync('showPayment', res.showPayment)
					uni.setStorageSync('userNewCalendar', res.userNewCalendar)

					uni.setStorageSync('maxImageMegBytes', res.maxImageMegBytes)
					uni.setStorageSync('maxVideoMegBytes', res.maxVideoMegBytes)
					uni.setStorageSync('systemUser', res.systemUser)



					let menberdata = uni.getStorageSync('menberdata') ? uni.getStorageSync('menberdata') : ''
					uni.hideLoading()
					if (menberdata) {
						console.log(menberdata)

						this.$api.acceptInvite(menberdata).then(res => {
							this.$Show.show('家庭加入成功')
							uni.removeStorageSync('menberdata')
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							}, 1500)
						})
					} else {
						console.log(res)
						uni.reLaunch({
							url: '/pages/tabbar/index/index'
						})
						// if(!res.familyIds || res.familyIds.length == 0){
						// 	uni.reLaunch({
						// 		url:'/pages/tabbar/family/createFamily'
						// 	})
						// }else{


						// }
					}



				})


			},

		}
	};
</script>

<style lang="scss">
	.registerText {
		color: #fff;
	}

	.login_form {
		background-color: #fff;
		margin-top: 30rpx;

		.list {
			display: flex;
			align-items: center;
			height: 100rpx;
			border-bottom: 1px solid #f8f8f8;

			.label {
				width: 170rpx;
				text-align: right;
				margin-right: 20rpx;
			}
		}

		.list:last-child {
			border: none;
		}
	}

	.agree {
		display: flex;
		align-items: center;
		padding-left: 30rpx;
		margin: 30rpx 0;

		image {
			width: 45rpx;
			height: 45rpx;
			margin-right: 10rpx;
		}

		span {
			text-decoration: underline;
		}
	}

	.loginBtn {
		width: 690rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		margin: 0 auto;
		margin-top: 40rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
	}

	.forget {
		width: 690rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 auto;
		margin-top: 20rpx;
	}

	.otherLogin {
		width: 690rpx;
		margin: 0 auto;
		margin-top: 30rx;
		margin-bottom: 40rpx;

		.btns {
			display: flex;
			align-items: center;
			justify-content: center;

			view {
				width: 300rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				border-radius: 10rpx;
				background-color: #06b2c0;
				position: relative;
			}

			.getUserInfobtn {
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 100;
				opacity: 0;
			}

			image {
				width: 50rpx;
				height: auto;
				margin-right: 10rpx;
			}
		}
	}
	
	.getNumber{
		height: 370rpx;
		background-color: #f8f8f8;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		box-sizing: border-box;
		padding: 30rpx;
		.title{
			display: flex;
			align-items: center;
			margin-bottom: 50rpx;
			image{
				width: 40rpx;
				height: auto;
				border-radius: 50%;
				margin-right: 10rpx;
			}
		}
		.btns{
			display: flex;
			align-items: center;
			margin-top: 70rpx;
			span{
				width: 330rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #fff;
				color: #08C060;
				border-radius: 10rpx;
				position: relative;
			}
			.act{
				background-color: #08C060;
				color: #fff;
			}
			.getPhoneNumberbss{
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 10;
				opacity: 0;
				
			}
		}
	}
</style>